<?php
/**
 * 商品模型
 */
class GoodsModel extends Model {
	public $tabele = 'goods';
	/**
	 * 自动验证
	 */
	public $validate = array( array('gname', 'notnull', '商品名称不能为空！', 2, 3));
	/**
	 * 自动完成
	 */
	public $auto = array( array('addtime', 'strtotime', 'function', 2, 3), array('index_image', '_index_image', 'method', 2, 3), array('list_image', '_list_image', 'method', 2, 3), );
	/**
	 * 处理首页上传图片
	 */
	public function _index_image($v) {
		$upload = new Upload();
		$file = $upload -> upload('index_image');
		return empty($file) ? $v : $file[0]['path'];
	}

	/**
	 * 处理列表页上传图片
	 */
	public function _list_image($v) {
		$upload = new Upload();
		$file = $upload -> upload('list_image');
		return empty($file) ? $v : $file[0]['path'];
	}

	/**
	 * 添加商品
	 */
	public function addGoods() {
		if ($this -> create()) {
			if ($gid = $this -> add()) {
				$upload = new Upload();
				$images = $upload -> upload('image');
				$thumbs = new Image();
				$data = array();
				foreach ($images as $img) {
					$data['small'] = $thumbs -> thumb($img['path'], $img['dir'] . $img['filename'] . '_small.' . $img['ext'], 50, 50);
					$data['medium'] = $thumbs -> thumb($img['path'], $img['dir'] . $img['filename'] . '_medium.' . $img['ext'], 350, 350);
					$data['big'] = $img['path'];
					$data['gid'] = $gid;
					M('goods_image')->add($data);
				}
				return true;
			}
		}
	}

}
